package org.jeecg.qis.service.impl;

import org.jeecg.qis.entity.QisSpcheadCfg;
import org.jeecg.qis.entity.QisSpcdetailCfg;
import org.jeecg.qis.mapper.QisSpcdetailCfgMapper;
import org.jeecg.qis.mapper.QisSpcheadCfgMapper;
import org.jeecg.qis.service.IQisSpcheadCfgService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 质控点配置
 * @Author: LiCY
 * @Date:   2023-06-28
 * @Version: V1.0
 */
@Service
public class QisSpcheadCfgServiceImpl extends ServiceImpl<QisSpcheadCfgMapper, QisSpcheadCfg> implements IQisSpcheadCfgService {

	@Autowired
	private QisSpcheadCfgMapper qisSpcheadCfgMapper;
	@Autowired
	private QisSpcdetailCfgMapper qisSpcdetailCfgMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(QisSpcheadCfg qisSpcheadCfg, List<QisSpcdetailCfg> qisSpcdetailCfgList) {
		qisSpcheadCfgMapper.insert(qisSpcheadCfg);
		if(qisSpcdetailCfgList!=null && qisSpcdetailCfgList.size()>0) {
			for(QisSpcdetailCfg entity:qisSpcdetailCfgList) {
				//外键设置
				entity.setHeadid(qisSpcheadCfg.getRecId());
				qisSpcdetailCfgMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(QisSpcheadCfg qisSpcheadCfg,List<QisSpcdetailCfg> qisSpcdetailCfgList) {
		qisSpcheadCfgMapper.updateById(qisSpcheadCfg);
		
		//1.先删除子表数据
		qisSpcdetailCfgMapper.deleteByMainId(qisSpcheadCfg.getRecId().toString());
		
		//2.子表数据重新插入
		if(qisSpcdetailCfgList!=null && qisSpcdetailCfgList.size()>0) {
			for(QisSpcdetailCfg entity:qisSpcdetailCfgList) {
				//外键设置
				entity.setHeadid(qisSpcheadCfg.getRecId());
				qisSpcdetailCfgMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		qisSpcdetailCfgMapper.deleteByMainId(id);
		qisSpcheadCfgMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			qisSpcdetailCfgMapper.deleteByMainId(id.toString());
			qisSpcheadCfgMapper.deleteById(id);
		}
	}
	
}
